﻿using Newtonsoft.Json;
using RTW.OA.Areas.Common.Servers;
using RTW.OA.Areas.System.Servers;
using RTW.OA.Attributes;
using RTW.OA.Controllers;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.Document.Controllers
{
    [CheckLogin(IsChildPage = true)]
    public class ReceiveController : BaseController
    {
        private DocumentReceiveServers documentReceiveServers;
        private PositionServers positionServers;

        public ReceiveController()
        {
            documentReceiveServers = new DocumentReceiveServers();

            positionServers = new PositionServers();
        }

        // GET: Document/Receive
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Sign(int id)
        {
            var model = documentReceiveServers.GetModelById(id);

            string[] URGENT_LEVEL_LIST = { "普通", "平急", "加急", "特急", "特提" };
            string[] SECRET_LEVEL_LIST = { "非密", "秘密", "机密", "绝密" };
            string[] DOC_CATEGORY_LIST = { "要求", "通知", "公文", "文件" };

            ViewBag.TEMPLATE_LIST = this.GetSelectList(new string[] { "本地模板" });
            ViewBag.URGENT_LEVEL_LIST = this.GetSelectList(URGENT_LEVEL_LIST, model.official_document.URGENT_LEVEL);
            ViewBag.SECRET_LEVEL_LIST = this.GetSelectList(SECRET_LEVEL_LIST, model.official_document.SECRET_LEVEL);
            ViewBag.DOC_CATEGORY_LIST = this.GetSelectList(DOC_CATEGORY_LIST, model.official_document.DOC_CATEGORY);

            return View(model);
        }

        [HttpPost]
        public ActionResult Sign(official_document_receive model)
        {
            model.RECEIVE_TIME = DateTime.Now;
            model.STATUS = 1;
            documentReceiveServers.Update(model);

            string[] URGENT_LEVEL_LIST = { "普通", "平急", "加急", "特急", "特提" };
            string[] SECRET_LEVEL_LIST = { "非密", "秘密", "机密", "绝密" };
            string[] DOC_CATEGORY_LIST = { "要求", "通知", "公文", "文件" };

            ViewBag.TEMPLATE_LIST = this.GetSelectList(new string[] { "本地模板" });
            ViewBag.URGENT_LEVEL_LIST = this.GetSelectList(URGENT_LEVEL_LIST, model.official_document.URGENT_LEVEL);
            ViewBag.SECRET_LEVEL_LIST = this.GetSelectList(SECRET_LEVEL_LIST, model.official_document.SECRET_LEVEL);
            ViewBag.DOC_CATEGORY_LIST = this.GetSelectList(DOC_CATEGORY_LIST, model.official_document.DOC_CATEGORY);

            return View(model);
        }

        public string GetDataTableJsonResult()
        {
            var datalist = documentReceiveServers.GetDataByUserNO(this.CurrentUser.USER_NO);
            return JsonConvert.SerializeObject(datalist);
        }

        private IEnumerable<SelectListItem> GetSelectList(string[] options, string defaultValue = null)
        {
            var list = new List<SelectListItem>();
            foreach (var item in options)
            {
                var selectListItem = new SelectListItem
                {
                    Text = item,
                    Value = item,
                    Selected = (defaultValue != null ? item == defaultValue : false)
                };
                list.Add(selectListItem);
            }
            return list;
        }

        // GET: Document/Receive/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Document/Receive/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Document/Receive/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Document/Receive/Edit/5
        public ActionResult Edit(int? id)
        {
            ViewBag.TEMPLATE = this.GetSelectList(new string[] { "本地模板" });
            if (id.HasValue)
            {
                ViewBag.Title = "编辑发文";
                var model = documentReceiveServers.GetModelById(id.Value);

                //ViewBag.URGENT_LEVEL_LIST = this.GetSelectList(new string[] { "普通", "平急", "加急", "特急", "特提" },model.URGENT_LEVEL);

                //ViewBag.SECRET_LEVEL_LIST = this.GetSelectList(new string[] { "非密", "秘密", "机密", "绝密" },model.SECRET_LEVEL);

                //ViewBag.DOC_CATEGORY_LIST = this.GetSelectList(new string[] { "要求", "通知", "公文", "文件" },model.DOC_CATEGORY);

                return View(model);
            }
            else
            {

                ViewBag.URGENT_LEVEL_LIST = this.GetSelectList(new string[] { "普通", "平急", "加急", "特急", "特提" });

                ViewBag.SECRET_LEVEL_LIST = this.GetSelectList(new string[] { "非密", "秘密", "机密", "绝密" });

                ViewBag.DOC_CATEGORY_LIST = this.GetSelectList(new string[] { "要求", "通知", "公文", "文件" });

                ViewBag.Title = "新建发文";
                return View();
            }
        }

        public ActionResult SelectDataRight(string id)
        {
            ViewBag.right = id ?? "";
            return View();
        }


        public ActionResult GetjsonTreeData(string idstr = null)
        {
            var list = GetDepts(0, idstr);
            return Json(list, JsonRequestBehavior.AllowGet);
        }


        public List<TreeViewNode> GetDepts(int parentId, string idstr)
        {
            List<TreeViewNode> tlist = new List<TreeViewNode>();
            var list = positionServers.GetDepts();
            var deptlist = list.FindAll(m => m.DEPT_PARENT == parentId);
            if (deptlist != null && deptlist.Count > 0)
            {
                foreach (var item in deptlist)
                {
                    TreeViewNode treeViewNode = new TreeViewNode
                    {
                        text = item.DEPT_NAME,
                        id = "D" + item.DEPT_ID.ToString(),
                        icon = "glyphicon glyphicon-equalizer",
                        nodes = new List<TreeViewNode>()
                    };
                    var personlist = GetPerson(item.DEPT_ID, idstr);
                    if (personlist != null && personlist.Count > 0)
                    {
                        treeViewNode.nodes.AddRange(personlist);
                    }
                    var subdeptlist = GetDepts(item.DEPT_ID, idstr);
                    if (subdeptlist != null && subdeptlist.Count > 0)
                    {
                        treeViewNode.nodes.AddRange(subdeptlist);
                    }
                    if (!string.IsNullOrEmpty(idstr))
                    {
                        if (idstr.Contains("D" + item.DEPT_ID.ToString()))
                        {
                            treeViewNode.state = new State { @checked = true };
                        }

                    }

                    tlist.Add(treeViewNode);
                }
            }
            return tlist;

        }

        public List<TreeViewNode> GetPerson(int parentId, string idstr)
        {
            List<TreeViewNode> tlist = new List<TreeViewNode>();
            var list = positionServers.GetUsers().FindAll(m => m.DEPT_ID == parentId);
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    TreeViewNode treeViewNode = new TreeViewNode
                    {
                        text = item.TRUE_NAME,
                        id = "U" + item.USER_ID.ToString(),
                        icon = "glyphicon glyphicon-user"
                    };
                    if (!string.IsNullOrEmpty(idstr))
                    {
                        if (idstr.Contains("U" + item.DEPT_ID.ToString()))
                        {
                            treeViewNode.state = new State { @checked = true };
                        }

                    }
                    tlist.Add(treeViewNode);
                }
            }
            return tlist;
        }


        // POST: Document/Receive/Edit/5
        [HttpPost]
        public ActionResult Edit(official_document_receive model)
        {
            try
            {
                if (model.DOC_ID == 0)
                {
                    model.CREATE_TIME = DateTime.Now;
                    documentReceiveServers.Add(model);
                    ViewBag.msg = "新建发文保存成功！";
                    model = new official_document_receive();

                }
                else
                {
                    model.CREATE_TIME = DateTime.Now;
                    documentReceiveServers.Update(model);
                    ViewBag.msg = "发文修改成功！";
                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }
            return RedirectToAction("Index");
        }

        // GET: Document/Receive/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Document/Receive/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
